﻿using System.IO;
using System;

namespace Library.WebControls.SimpleCallback {
	public class CustomDownloadEventArgs : CustomCallbackEventArgs {
		public CustomDownloadEventArgs(string parameter)
			: base(parameter) {
			ContentType = "application/octet-stream";
		}
		/// <summary>
		/// File Path
		/// </summary>
		public string FileName { get; set; }
		public byte[] FileContent { get; set; }
		public string ContentType { get; set; }
		public bool Error { get { return !String.IsNullOrEmpty(ErrorText); } }
		public string ErrorText { get; set; }
		public bool DeleteFile { get; set; }
		public void ReadFromStream(Stream stream) {
			if (stream == null) return;
			if (stream.Length == 0) return;
			var len = (Int32)stream.Length;
			if (stream.CanSeek) stream.Seek(0, SeekOrigin.Begin);
			int byteRead = 0;
			var bs = new byte[len];
			do {
				var read = stream.Read(bs, byteRead, len - byteRead);
				if (read == 0) {
					throw new EndOfStreamException();
				}
				byteRead += read;
			} while (byteRead < len);


			FileContent = bs;
		}

		//public bool DownloadLater { get; set; }
	}
}
